﻿using IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Repositories;
using ViewModels.RecruitInfoVM;

namespace HinokyWeb.Controllers
{
    public class RecruitInfoController : Controller
    {
        private IRecruitStateRepository _recruitStateRepository;

        public RecruitInfoController()
        {
            _recruitStateRepository = new RecruitStateRepository();
        }

        public ActionResult Index()
        {
            var resultData = new RecruitInfoMainVM(); //RecruitInfoListForGrid

            var dataList = _recruitStateRepository.GetRecruitState(resultData.StartDate, resultData.EndDate, string.Empty, string.Empty);

            foreach (var data in dataList.GroupBy(s=>s.BasicDate))
            {
                resultData.RecruitInfoListForGrid.Add(new RecruitInfoVM
                {
                    BaseDate = data.Key,
                });
            }

            foreach (var resultItem in resultData.RecruitInfoListForGrid)
            {
                resultItem.DevWebNet = dataList.Where(s => s.BasicDate == resultItem.BaseDate && s.AgentType == "S" && s.JobType == "DEV-WEB-NET").Select(s => s.RecruitCount).FirstOrDefault();
                resultItem.DevWebJava = dataList.Where(s => s.BasicDate == resultItem.BaseDate && s.AgentType == "S" && s.JobType == "DEV-WEB-JAVA").Select(s => s.RecruitCount).FirstOrDefault();
                resultItem.DevWebTotal = resultItem.DevWebNet + resultItem.DevWebJava;
            }

            return View(resultData);
        }

        [HttpGet]
        public ActionResult GetData()
        {
            var resultData = new RecruitInfoMainVM();

            var data = _recruitStateRepository.GetRecruitState(resultData.StartDate,
                resultData.EndDate, string.Empty, string.Empty);

            foreach (var recruitInfoSeries in resultData.RecruitInfoSeriesListForChart)
            {
                var searchDataList = data.Where(s => s.JobType == recruitInfoSeries.JobType && s.AgentType == recruitInfoSeries.AgentType).OrderBy(s => s.BasicDate).Select(s => s.RecruitCount).ToList();

                recruitInfoSeries.RecruitCounts = searchDataList;
            }

            for (var i = Convert.ToDateTime(resultData.StartDate); Convert.ToDateTime(resultData.EndDate).CompareTo(i) >= 0 ; i = i.AddDays(1))
            {
                resultData.XAxisList.Add(i.ToString("yyyy-MM-dd"));
            }

            return Json(new { ResultData = resultData }, JsonRequestBehavior.AllowGet);
        }
    }
}
